'''
Created on 01/11/2012

@author: rcasal
'''

from repositories.i_repository import IRepository
from gluon.dal import Field
from domain.entities.mail import MailBandejaEntrada, MailConsulta, MailRespuesta


class BandejaEntradaRepository(IRepository):
    '''
    classdocs
    '''

    def __init__(self, sigecon, T):
        '''
        Constructor
        '''
        self.__sigecon = sigecon
        self.__table = None
        self.__T = T
        
    def define_table(self, migrate = False):
        self.__sigecon.define_table('BandejaDeEntrada',
            Field('user_server', type = 'string', required = True, notnull= True, label = 'Cuenta'),
            Field('id_mail', type = 'string', required = True, notnull= True, unique = True, label = 'ID_Mail'),
            Field('Date_', type = 'datetime', required = True, notnull= True, label = 'Date'),
            Field('From_', type = 'string', required = True, notnull= True, label = 'From'),
            Field('To_', type = 'string', required = True, notnull= True, label = 'To'),
            Field('Subject_', type = 'string', required = True, notnull= True, label = 'Subject'),
            Field('Message_', type = 'text', required = True, notnull= True, label = 'Message'),
            Field('leido', type = 'boolean', required = True, notnull= True, label = 'Leido'),
            Field('fecha_leido', type = 'datetime', required = True, notnull= False, label = 'Fecha Descarga'),
            migrate = migrate
        )
        
        self.__table = self.__sigecon['BandejaDeEntrada']

        
    def add_requires(self):
        raise NotImplementedError()
 
    def add_represents(self):
        raise NotImplementedError()
    
    def findOne(self, ident):
        fields =  self.__sigecon(self.__sigecon.BandejaDeEntrada.id == ident).select().last() 
        if fields:
            return MailBandejaEntrada.deserialize(fields)
        else:
            return None  
    
    def findAll(self):
        res = self.__sigecon(self.__sigecon.BandejaDeEntrada.id > 0).select() 
        mails = []
        for fields in res:
            mails.append(MailBandejaEntrada.deserialize(fields))
        return mails
    
    def findOneByID_Mail(self, id_mail):
        fields =  self.__sigecon(self.__sigecon.BandejaDeEntrada.id_mail == id_mail).select().last() 
        if fields:
            return MailBandejaEntrada.deserialize(fields)
        else:
            return None  
    
    def save(self, entity):
        mail = MailBandejaEntrada #@UnusedVariable
        mail = entity
        print mail.Fecha
        id_ent = self.__sigecon.BandejaDeEntrada.insert(
                user_server = mail.Account,
                id_mail = mail.ID_mail,
                Date_ = mail.Fecha,
                From_ = mail.From,
                To_ = mail.To,
                Subject_ = mail.Subject,
                Message_ = mail.Text,
                leido = mail.Leido,
                fecha_leido = mail.Fecha_Leido,
        )
        return id_ent
        
    
    def delete(self, ident):
        self.__sigecon(self.__sigecon.BandejaDeEntrada.id == ident).delete()
    
    def update(self, entity):
        record = self.__sigecon(self.__sigecon.BandejaDeEntrada.id == entity.ID).select().first()
        mail = MailBandejaEntrada #@UnusedVariable
        mail = entity
        record.update_record(
                user_server = mail.Account,
                id_mail = mail.ID_mail,
                Date_ = mail.Fecha,
                From_ = mail.From,
                To_ = mail.To,
                Subject_ = mail.Subject,
                Message_ = mail.Text,
                leido = mail.Leido,
                fecha_leido = mail.Fecha_Leido,
        )
    
class MailConsultaRepository(IRepository):
    '''
    classdocs
    '''

    def __init__(self, sigecon, T):
        '''
        Constructor
        '''
        self.__sigecon = sigecon
        self.__table = None
        self.__T = T
        
    def define_table(self, migrate = False):
        self.__sigecon.define_table('MailsConsulta',
        Field('consulta', type = 'reference Consultas', required = True, notnull = True, label = 'Consulta'),
        Field('user_server', type = 'string', required = True, notnull = True, label = 'Cuenta'),
        Field('id_mail', type = 'string', required = True, notnull = True, label = 'Identifier'),
        Field('From_', type = 'string', required = True, notnull = True, label = 'From'),
        Field('To_', type = 'string', required = True, notnull = True, label = 'To'),
        Field('Subject_', type = 'string', required = True, notnull = True, label = 'Subject'),
        Field('Message_', type = 'text', required = True, notnull = True, label = 'Contenido'),
        Field('Date_', type = 'datetime', required = True, notnull = True, label = 'Fecha'),
        Field('fecha_leido', type = 'datetime', required = True, notnull = True, label = 'Fecha Descarga'),
        migrate = migrate
        )
        
        self.__table = self.__sigecon['MailsConsulta']

        
    def add_requires(self):
        raise NotImplementedError()
 
    def add_represents(self):
        raise NotImplementedError()
    
    def findOne(self, ident):
        fields =  self.__sigecon(self.__sigecon.MailsConsulta.id == ident).select().last() 
        if fields:
            return MailConsulta.deserialize(fields)
        else:
            return None  
    
    def findAll(self):
        res = self.__sigecon(self.__sigecon.MailsConsulta.id > 0).select() 
        mails = []
        for fields in res:
            mails.append(MailConsulta.deserialize(fields))
        return mails 
    
    def findByIdConsulta(self, idconsulta):
        res = self.__sigecon(self.__sigecon.MailsConsulta.consulta == idconsulta).select() 
        mails = []
        for fields in res:
            mails.append(MailConsulta.deserialize(fields))
        return mails 
    
    def findByFrom(self, from_):
        res = self.__sigecon(self.__sigecon.MailsConsulta.From_ == from_).select() 
        mails = []
        for fields in res:
            mails.append(MailConsulta.deserialize(fields))
        return mails 
    
    def save(self, entity):
        mail = MailConsulta #@UnusedVariable
        mail = entity
        id_ent = self.__sigecon.MailsConsulta.insert(
                user_server = mail.Account,
                id_mail = mail.ID_mail,
                Date_ = mail.Fecha,
                From_ = mail.From,
                To_ = mail.To,
                Subject_ = mail.Subject,
                Message_ = mail.Text,
                fecha_leido = mail.Fecha_Leido,
                consulta = mail.idConsulta
        )
        return id_ent
        
    
    def delete(self, entity):
        raise NotImplementedError()
    
    def update(self, entity):
        raise NotImplementedError()
    
    
class MailRespuestaRepository(IRepository):
    '''
    classdocs
    '''

    def __init__(self, sigecon, T):
        '''
        Constructor
        '''
        self.__sigecon = sigecon
        self.__table = None
        self.__T = T
        
    def define_table(self, migrate = False):
        self.__sigecon.define_table('MailsRespuesta',
        Field('mail_consulta', type = 'reference MailsConsulta', unique = True, required = True, notnull = True),
        Field('Message_', type = 'text', required = True, notnull = True, label = 'Contenido'),
        Field('fecha_envio', type = 'datetime', required = True, notnull = True, label = 'Fecha'),
        migrate = migrate
        )
        
        self.__table = self.__sigecon['MailsRespuesta']

        
    def add_requires(self):
        raise NotImplementedError()
 
    def add_represents(self):
        raise NotImplementedError()
    
    def findOne(self, ident):
        fields =  self.__sigecon(self.__sigecon.MailsRespuesta.id == ident).select().last() 
        if fields:
            return MailRespuesta.deserialize(fields)
        else:
            return None  
    
    def findAll(self):
        res = self.__sigecon(self.__sigecon.MailsRespuesta.id > 0).select() 
        mails = []
        for fields in res:
            mails.append(MailRespuesta.deserialize(fields))
        return mails 
    
    def findByIdMailConsulta(self, idmail):
        fields = self.__sigecon(self.__sigecon.MailsRespuesta.mail_consulta == idmail).select().last()
        if fields:
            return MailRespuesta.deserialize(fields)
        else:
            return None
    
    def save(self, entity):
        mail = MailRespuesta #@UnusedVariable
        mail = entity
        print mail.Fecha
        self.__sigecon.MailsRespuesta.insert(
                mail_consulta = mail.mail_consulta.ID,
                Message_ = mail.Text,
                fecha_envio = mail.Fecha,
        )
        
    
    def delete(self, entity):
        raise NotImplementedError()
    
    def update(self, entity):
        raise NotImplementedError()